/*
 * Copyright (c) 2000-2017 wanglei. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */

package com.xhb.ui;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.UUID;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.teamdev.jxbrowser.deps.com.google.common.collect.Lists;
import com.xhb.ui.tools.BaiduUtil;
import com.xhb.ui.tools.LogFormat;
import com.xhb.ui.tools.PNGBitmapConverter;
import com.xhb.ui.tools.PicUtil;
import com.xhb.ui.tools.PopleImgUtil;
import com.xhb.ui.tools.VideoUtil;

import cn.hutool.core.io.FileUtil;

/**
 * @author tanyukun.
 */
public class ImgGenFrame extends JFrame implements ActionListener {

    JButton loginButton, exitButton, logoutButton,logoutButton1,baiduCheck,baiduImgCheck,newbackImgButton,hbbmpButton;
    JTextField userNameField;
    JTextField randomField;
    JPasswordField passwordField;

    /**
     * 标签页的登录按钮
     */
    ToolBar.ActionButton loginActButton;


    public ImgGenFrame() {
        //设置窗体宽高
        this.setSize(550, 200);
        //设置窗口居中显示
        this.setLocationRelativeTo(null);
        //设置关闭窗口结束程序
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //设置窗体禁止调节大小
        this.setResizable(false);
        //创建面板
        JPanel jPanel = new JPanel();
        
        loginFrame(jPanel);



    }

    private void loginFrame(JPanel jPanel) {

        this.setTitle("合成背景图片");

        //创建JLabel(用户名)
        JLabel userLabel = new JLabel("文件目录:");
        userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        //定义组件的位置和宽高
        userLabel.setBounds(10, 20, 80, 25);
        //把组件添加到JPanel上
        jPanel.add(userLabel);

        //创建文不域用于用户输入
        userNameField = new JTextField(20);
        //设置文本域的位置和宽高
        userNameField.setBounds(100, 20, 165, 25);
        //把文本域组件添加上
        jPanel.add(userNameField);
        
        
        //创建文不域用于用户输入
        randomField = new JTextField(20);
        //设置文本域的位置和宽高
        randomField.setBounds(40, 40, 25, 25);
        
        randomField.setText("0");
        //把文本域组件添加上
        jPanel.add(randomField);

        
        exitButton = new JButton("背景透明");
        exitButton.setBounds(0, 180, 80, 25);
        exitButton.addActionListener(this);
        jPanel.add(exitButton);
        

        //登录按钮
        loginButton = new JButton("生成背景");
        loginButton.setBounds(80, 100, 80, 25);
        loginButton.addActionListener(this);
        jPanel.add(loginButton);
        
        newbackImgButton = new JButton("生成新背景");
        newbackImgButton.setBounds(80, 100, 80, 25);
        newbackImgButton.addActionListener(this);
        jPanel.add(newbackImgButton);
        
        //登录按钮
        baiduImgCheck = new JButton("背景图检测");
        baiduImgCheck.setBounds(80, 100, 80, 25);
        baiduImgCheck.addActionListener(this);
        jPanel.add(baiduImgCheck);
       
        
        logoutButton1 = new JButton("生成视频");
        logoutButton1.setBounds(0, 100, 80, 25);
        logoutButton1.addActionListener(this);
        jPanel.add(logoutButton1);
        
        baiduCheck = new JButton("视频人脸检测");
        baiduCheck.setBounds(0, 100, 80, 25);
        baiduCheck.addActionListener(this);
        jPanel.add(baiduCheck);
        
//        hbbmpButton = new JButton("背景图转bmp格式");
//        hbbmpButton.setBounds(0, 100, 80, 25);
//        hbbmpButton.addActionListener(this);
//        jPanel.add(hbbmpButton);
       
        

        //添加面板
        this.add(jPanel);
        //设置可见
        this.setVisible(true);
    }

    
  
    
	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == newbackImgButton) {

			// 备份背景图目录
			File fileDir = new File(userNameField.getText() + "\\n");
			if (fileDir.exists()) {
				FileUtil.rename(fileDir, fileDir.getPath() + UUID.randomUUID().toString(), true);
			}

			// 备份视频目录
			fileDir = new File(userNameField.getText() + "\\video");
			if (fileDir.exists()) {
				FileUtil.rename(fileDir, fileDir.getPath() + UUID.randomUUID().toString(), true);
			}

			PicUtil.createImg(userNameField.getText(), randomField.getText());
		}

		if (e.getSource() == loginButton) {

			PicUtil.createImg(userNameField.getText(), randomField.getText());

			JOptionPane.showMessageDialog(null, "生成成功");
		}

		if (e.getSource() == hbbmpButton) {
			PNGBitmapConverter.converter(userNameField.getText());
			
		}

		if (e.getSource() == logoutButton1) {
			/**
			 * 异步生成不阻塞其他操作
			 */
			new Thread(new Runnable() {
				@Override
				public void run() {
					VideoUtil.sync(userNameField.getText());

					LogFormat.info("video check success");
				}
			}).start();
		}

		if (e.getSource() == baiduImgCheck) {

			BaiduUtil.checkImg(userNameField.getText());

			JOptionPane.showMessageDialog(null, "检测完成");
		}

		if (e.getSource() == baiduCheck) {
			/**
			 * 异步生成不阻塞其他操作
			 */
			new Thread(new Runnable() {
				@Override
				public void run() {
					BaiduUtil.check(userNameField.getText());

					LogFormat.info("image face check success");
				}
			}).start();
		}

		if (e.getSource() == exitButton) {
			java.util.List<File> popleFiles = Lists.newArrayList();
			String userName = userNameField.getText();
			File file1 = new File(userName + "\\1.png");
			if (file1.exists()) {
				popleFiles.add(file1);
			}
			File file2 = new File(userName + "\\2.png");
			if (file1.exists()) {
				popleFiles.add(file2);
			}
			File file3 = new File(userName + "\\3.png");
			if (file3.exists()) {
				popleFiles.add(file3);
			}
			if (popleFiles.isEmpty()) {
				JOptionPane.showMessageDialog(null, "人像图片最少一张 命名 1.png，2.png以此类推");
				return;
			}

			for (File file : popleFiles) {
				PopleImgUtil.foreground(file.getPath());
			}

			JOptionPane.showMessageDialog(null, "抠图成功");
		}
	}
	
	
	public static  void main(String [] args) {
		File file1 = new File("C:\\zgymw\\浙江学员名单\\陈迁-土建质量员-532128199901230339\\n");
		java.util.List<String> array=Lists.newArrayList();
		for (File file : file1.listFiles()) {
			array.add(file.getPath().replace(file1.getPath(), "").replace("\new", "").replace(".png", ""));
		}
		Collections.sort(array, new Comparator<String>() {
			public int compare(String o1, String o2) {
				return Integer.parseInt(o1) - Integer.parseInt(o2);
			};
		});
		for (String string : array) {
			System.out.println("new"+string+".png");
		}
	}
}
